﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WeChartWorkRobot.Common;
using WeChartWorkRobot.Model;
using WeChartWorkRobot.Service;
using WeChartWorkRobot.Service.Jobs;

namespace WeChartWorkRobot.Service.CustomerMsg
{
    public class HighDataFeedBackByEveryDay : SendCustomerMsgBase
    {
        public override string getContent()
        {
            Console.WriteLine("HighDataFeedBackByEveryDay定时任务执行");
            LogHelper.Debug("HighDataFeedBackByEveryDay定时任务执行");
            
            
            string startDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            string endDate = DateTime.Now.ToString("yyyy-MM-dd");

            string queryStr = jiraConfig.Query.Replace("1=1", $"created >= {startDate} AND created < {endDate}");

            queryStr = UtilityService.UrlEncode(queryStr);
            
            CookieContainer Cookie = jiraConfig.Login();
            JiraBusinessService globalBusinessService = new JiraBusinessService(Cookie);
            List<JiraIssue> jiraIssues = globalBusinessService.Query(jiraConfig.JiraHost,queryStr);



            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"{DateTime.Now.AddDays(-1).Day}号反馈Jira问题共<font color=\"warning\">{jiraIssues.Count}</font>条，已关闭<font color=\"info\">{jiraIssues.FindAll(p => p.fields.status.name == "已关闭").Count()}</font>条");

            foreach (JiraIssue jiraIssue in jiraIssues)
            {
                string statusColor = jiraIssue.fields.status.name == "已关闭" ? "info" : "warning";
                string linkUrl = jiraConfig.JiraHost+ "/browse/" + jiraIssue.key;
                string reporter = jiraIssue.fields.reporter == null ? "无" : jiraIssue.fields.reporter.displayName;
                string jbr = jiraIssue.fields.customfield_10700 == null ? "无" : jiraIssue.fields.customfield_10700.displayName;
                stringBuilder.AppendLine($">【[{jiraIssue.key}]({linkUrl})】({jiraIssue.fields.issuetype.name})；{reporter}；<font color=\"{statusColor}\">{jiraIssue.fields.status.name}</font>；处理人：{jbr}。");
            }

            return stringBuilder.ToString();
        }

    }

}
